/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaAdesaoConvenio.java
 *
 * Created on 02/11/2010, 17:47:32
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.ConvenioPorAssociado;
import asfabdesk.dominio.ConvenioPorDependente;
import asfabdesk.dominio.Data;
import asfabdesk.presistencia.DAO;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author marco.machado
 */
public class TelaAdesaoConvenio extends javax.swing.JInternalFrame {

    public ConvenioPorAssociado convenioPorAssociado;
    public ConvenioPorDependente convenioPorDependente;

    /** Creates new form TelaAdesaoConvenio */
    public TelaAdesaoConvenio(ConvenioPorAssociado convenio) {
        initComponents();
        convenioPorAssociado = convenio;
        configurarTelaAssociado(convenioPorAssociado);
    }

    public TelaAdesaoConvenio(ConvenioPorDependente conv) {
        initComponents();
        convenioPorDependente = conv;
        configurarTelaDependente(convenioPorDependente);
    }

    public void configurarTelaAssociado(ConvenioPorAssociado conv) {
        txtConvenio.setText(conv.getConvenio().getNome());
        txtEmpresa.setText(conv.getConvenio().getEmpresa().getNomeFantasia());
        txtDescricao.setText(conv.getConvenio().getDescricao());
        txtTermino.setText(new Data().ConverteDataDateStringBra(conv.getConvenio().getDataTermino()));
        txtCarencia.setText(String.valueOf(conv.getConvenio().getCarencia()));
        txtAssociado.setText(conv.getAssociado().getNome());
        txtUsuario.setText(conv.getUsuarioCadastrante().getNome());

    }

    public void configurarTelaDependente(ConvenioPorDependente conv) {
        txtConvenio.setText(conv.getConvenio().getNome());
        txtEmpresa.setText(conv.getConvenio().getEmpresa().getNomeFantasia());
        txtDescricao.setText(conv.getConvenio().getDescricao());
        txtTermino.setText("");
        txtCarencia.setText("");
        txtAssociado.setText(conv.getDependente().getNome());
        txtUsuario.setText(conv.getUsuarioCadastrante().getNome());

    }

    public void salvarConvAssociado() throws ParseException {
        if (verificarDuplicidadeAssociado() == true) {
            try {
                java.util.Date data = (new Data()).ConverteDataHoraStringDate(txtData.getText());
                convenioPorAssociado.setDataRegistro(data);
                new DAO().salvar(convenioPorAssociado);
                JOptionPane.showMessageDialog(null, "Adesão realizada com Sucesso!", "Sucesso", 2);
                dispose();
            } catch (ParseException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Ops, data inválida!", "Data Inválida", 1);
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Ops, erro durante a adesão do convênio!", "Erro", 0);
            }
        } else {
            dispose();
        }
    }

    public void salvarConvDep() throws ParseException {
        if (verificarDuplicidadeDependente() == true) {
            try {
                java.util.Date data = (new Data()).ConverteDataHoraStringDate(txtData.getText());
                convenioPorDependente.setDataRegistro(data);
                new DAO().salvar(convenioPorDependente);
                JOptionPane.showMessageDialog(null, "Adesão realizada com Sucesso!", "Sucesso", 2);
                dispose();
            } catch (ParseException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Ops, data inválida!", "Data Inválida", 1);
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Ops, erro durante a adesão do convênio!", "Erro", 0);
            }
        } else {
            dispose();
        }
    }

    public boolean verificarDuplicidadeAssociado() {
        boolean retorno = true;
        if (new DAO().localizar(ConvenioPorAssociado.class, "ConvenioPorAssociadoDuplicado", convenioPorAssociado.getConvenio(), convenioPorAssociado.getAssociado()) != null) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Esse usuário já está cadastrado neste convênio! ", "Convênio Duplicado", 2);
        }
        return retorno;
    }

    public boolean verificarDuplicidadeDependente() {
        boolean retorno = true;
        if (new DAO().localizar(ConvenioPorDependente.class, "ConvenioPorDependenteDuplicado", convenioPorDependente.getConvenio(), convenioPorDependente.getDependente()) != null) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Esse usuário já está cadastrado neste convênio! ", "Convênio Duplicado", 2);
        }
        return retorno;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelPrincial = new javax.swing.JPanel();
        btnSim = new javax.swing.JButton();
        painelSuperior = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtConvenio = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtEmpresa = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        txtTermino = new javax.swing.JTextField();
        txtCarencia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        painelInferior = new javax.swing.JPanel();
        txtAssociado = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        txtData = new javax.swing.JFormattedTextField();
        jLabel27 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        btnNao = new javax.swing.JButton();

        setClosable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaAdesaoConvenio.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        painelPrincial.setBackground(resourceMap.getColor("painelPrincial.background")); // NOI18N
        painelPrincial.setName("painelPrincial"); // NOI18N
        painelPrincial.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnSim.setText(resourceMap.getString("btnSim.text")); // NOI18N
        btnSim.setName("btnSim"); // NOI18N
        btnSim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimActionPerformed(evt);
            }
        });
        painelPrincial.add(btnSim, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 350, 50, -1));

        painelSuperior.setBackground(resourceMap.getColor("painelSuperior.background")); // NOI18N
        painelSuperior.setName("painelSuperior"); // NOI18N
        painelSuperior.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel11.setIcon(resourceMap.getIcon("jLabel11.icon")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N
        painelSuperior.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 0, -1, -1));

        jLabel12.setIcon(resourceMap.getIcon("jLabel12.icon")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N
        painelSuperior.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 190, -1, -1));

        jLabel13.setFont(resourceMap.getFont("jLabel13.font")); // NOI18N
        jLabel13.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N
        painelSuperior.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel1.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        painelSuperior.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        txtConvenio.setBackground(resourceMap.getColor("txtConvenio.background")); // NOI18N
        txtConvenio.setEditable(false);
        txtConvenio.setText(resourceMap.getString("txtConvenio.text")); // NOI18N
        txtConvenio.setBorder(null);
        txtConvenio.setName("txtConvenio"); // NOI18N
        txtConvenio.setOpaque(false);
        painelSuperior.add(txtConvenio, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 220, -1));

        jLabel2.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        painelSuperior.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 50, -1));

        txtEmpresa.setText(resourceMap.getString("txtEmpresa.text")); // NOI18N
        txtEmpresa.setBorder(null);
        txtEmpresa.setName("txtEmpresa"); // NOI18N
        txtEmpresa.setOpaque(false);
        painelSuperior.add(txtEmpresa, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 60, 220, -1));

        jLabel3.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel3.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        painelSuperior.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescricao.setColumns(20);
        txtDescricao.setEditable(false);
        txtDescricao.setFont(resourceMap.getFont("txtDescricao.font")); // NOI18N
        txtDescricao.setLineWrap(true);
        txtDescricao.setRows(5);
        txtDescricao.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        txtDescricao.setName("txtDescricao"); // NOI18N
        jScrollPane1.getViewport().add(txtDescricao);

        painelSuperior.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 300, -1));

        jLabel4.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        painelSuperior.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, -1));

        txtTermino.setEditable(false);
        txtTermino.setText(resourceMap.getString("txtTermino.text")); // NOI18N
        txtTermino.setBorder(null);
        txtTermino.setName("txtTermino"); // NOI18N
        txtTermino.setOpaque(false);
        painelSuperior.add(txtTermino, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 180, 79, -1));

        txtCarencia.setEditable(false);
        txtCarencia.setBorder(null);
        txtCarencia.setName("txtCarencia"); // NOI18N
        txtCarencia.setOpaque(false);
        painelSuperior.add(txtCarencia, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 180, 24, -1));

        jLabel5.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel5.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        painelSuperior.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 180, -1, -1));

        jLabel6.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        painelSuperior.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 180, 70, 10));

        painelPrincial.add(painelSuperior, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 340, 210));

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N
        painelPrincial.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 350, -1, 20));

        painelInferior.setName("painelInferior"); // NOI18N
        painelInferior.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtAssociado.setBackground(resourceMap.getColor("txtAssociado.background")); // NOI18N
        txtAssociado.setEditable(false);
        txtAssociado.setBorder(null);
        txtAssociado.setName("txtAssociado"); // NOI18N
        txtAssociado.setOpaque(false);
        painelInferior.add(txtAssociado, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 40, 236, -1));

        jLabel24.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel24.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel24.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel24.setName("jLabel7"); // NOI18N
        painelInferior.add(jLabel24, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        jLabel25.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel25.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel25.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel25.setName("jLabel8"); // NOI18N
        painelInferior.add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        txtUsuario.setBackground(resourceMap.getColor("txtUsuario.background")); // NOI18N
        txtUsuario.setEditable(false);
        txtUsuario.setBorder(null);
        txtUsuario.setName("txtUsuario"); // NOI18N
        txtUsuario.setOpaque(false);
        painelInferior.add(txtUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 60, 237, -1));

        jLabel26.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel26.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel26.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel26.setName("jLabel9"); // NOI18N
        painelInferior.add(jLabel26, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));

        try {
            txtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtData.setName("txtData"); // NOI18N
        painelInferior.add(txtData, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, 80, -1));

        jLabel27.setName("jLabel27"); // NOI18N
        painelInferior.add(jLabel27, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 110, -1, -1));

        jLabel14.setFont(resourceMap.getFont("jLabel13.font")); // NOI18N
        jLabel14.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N
        painelInferior.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel28.setName("jLabel28"); // NOI18N
        painelInferior.add(jLabel28, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 0, -1, -1));

        painelPrincial.add(painelInferior, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, 340, 110));

        btnNao.setText(resourceMap.getString("btnNao.text")); // NOI18N
        btnNao.setName("btnNao"); // NOI18N
        btnNao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNaoActionPerformed(evt);
            }
        });
        painelPrincial.add(btnNao, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 350, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelPrincial, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelPrincial, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimActionPerformed
        if (convenioPorAssociado != null) {
            try {
                salvarConvAssociado();
            } catch (ParseException ex) {
                Logger.getLogger(TelaAdesaoConvenio.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (convenioPorDependente != null) {
            try {
                salvarConvDep();
            } catch (ParseException ex) {
                Logger.getLogger(TelaAdesaoConvenio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}//GEN-LAST:event_btnSimActionPerformed

    private void btnNaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNaoActionPerformed
        dispose();
    }//GEN-LAST:event_btnNaoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNao;
    private javax.swing.JButton btnSim;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel painelInferior;
    private javax.swing.JPanel painelPrincial;
    private javax.swing.JPanel painelSuperior;
    private javax.swing.JTextField txtAssociado;
    private javax.swing.JTextField txtCarencia;
    private javax.swing.JTextField txtConvenio;
    private javax.swing.JFormattedTextField txtData;
    private javax.swing.JTextArea txtDescricao;
    private javax.swing.JTextField txtEmpresa;
    private javax.swing.JTextField txtTermino;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
